package business_opportunity

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.myscrm.cn/golang/stark/v4"
)

type BuildingRepository struct {
}

func NewBuildingRepository() BuildingRepositoryIface {
	return &BuildingRepository{}
}

func (r *BuildingRepository) BuildingList(ctx context.Context, request *pb.BuildingListRequest) (*pb.BuildingListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "BuildingList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBuildingServiceClient(conn)
	res, err := client.BuildingList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "BuildingList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *BuildingRepository) ExportBuildingList(ctx context.Context, request *pb.ExportBuildingListRequest) (*pb.ExportBuildingListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ExportBuildingList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBuildingServiceClient(conn)
	res, err := client.ExportBuildingList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ExportBuildingList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *BuildingRepository) GetBuildingDetailBasic(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailBasicResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailBasic request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBuildingServiceClient(conn)
	res, err := client.GetBuildingDetailBasic(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailBasic request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *BuildingRepository) GetBuildingDetailTargetNum(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailTargetNumResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailTargetNum request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBuildingServiceClient(conn)
	res, err := client.GetBuildingDetailTargetNum(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailTargetNum request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *BuildingRepository) GetBuildingDetailBidList(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailBidListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailBidList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBuildingServiceClient(conn)
	res, err := client.GetBuildingDetailBidList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailBidList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *BuildingRepository) GetBuildingDetailParticipatingUnits(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailParticipatingUnitsResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailParticipatingUnits request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBuildingServiceClient(conn)
	res, err := client.GetBuildingDetailParticipatingUnits(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailParticipatingUnits request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *BuildingRepository) GetBuildingDetailContractList(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailContractListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailContractList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBuildingServiceClient(conn)
	res, err := client.GetBuildingDetailContractList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailContractList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *BuildingRepository) GetBuildingDetailWorkingDrawingAudit(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailWorkingDrawingAuditResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailWorkingDrawingAudit request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBuildingServiceClient(conn)
	res, err := client.GetBuildingDetailWorkingDrawingAudit(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailWorkingDrawingAudit request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *BuildingRepository) GetBuildingDetailConstructionPermit(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailConstructionPermitResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailConstructionPermit request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBuildingServiceClient(conn)
	res, err := client.GetBuildingDetailConstructionPermit(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailConstructionPermit request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *BuildingRepository) GetBuildingDetailCompletionFiling(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailCompletionFilingResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailCompletionFiling request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBuildingServiceClient(conn)
	res, err := client.GetBuildingDetailCompletionFiling(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailCompletionFiling request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}
